<?php
namespace Home\Controller;

/*
 * 文档管理..
 */
class DocumentController extends BaseController {

    private $path = "";

    public function __construct()
    {
        parent::__construct();
        $this -> path = "http://".$_SERVER['HTTP_HOST'].__ROOT__.'/';
        $this -> assign('cate' , 'document');
    }
    
    
    
    /*
     * 本项目的工程文档列表
     */
    public function document(){
        //init
        $column = C('documentColumn');
        $this -> assign('column' , $column);

        //
        if( I('get.id') ){
            S("projectid" , I('get.id'));
        }
        $projectid = I('get.id') ? I('get.id') : S("projectid");
        foreach($list as $v){
            if($v['id']==$projectid)
                $projectinfo = $v;
        }
        $this -> assign('projectinfo' , $projectinfo);

        //匹配
        if(I('get.stage')){
            S('stage' , I('get.stage'));
        }else{
            S('stage' , S('stage') ? S('stage') : 'all');
        }
        if(I('get.document')){
            S('document' , I('get.document'));
            S('cad' , null);
            S('mode' , null);
        }
        if(I('get.cad')){
            S('document' , null);
            S('cad' , I('get.cad'));
            S('mode' , null);
        }
        if(I('get.mode')){
            S('document' , null);
            S('cad' , null);
            S('mode' , I('get.mode'));
        }
        if(!S('document') && !S('cad') && !S('mode'))   S('document' , 'all');

        //where
        $where = "d.projectid='".$projectid."' and d.fileid=f.id and d.status=1";
        if(I('get.val')){
            $val = I('get.val');
            $where .= " and d.name like '%$val%'";
            $this -> assign('SearchValue' , $val);
        }
        if(S('stage')){
            $this -> assign('stage',S('stage'));
            if(S('stage') != 'all')
                $where .= " and d.stage='".S('stage')."'";
        }
        if(S('mode')){
            $this -> assign('mode',S('mode'));
            if(S('mode') == 'all')
                $where .= " and d.columntype='mode' ";
            else
                $where .= " and d.columntype='mode' and d.smalltype='".S('mode')."'";

        }
        if(S('cad')){
            $this -> assign('cad',S('cad'));
            if(S('cad') == 'all')
                $where .= " and d.columntype='cad'";
            else
                $where .= " and d.columntype='cad' and d.smalltype='".S('cad')."'";

        }
        if(S('document')){
            $this -> assign('document',S('document'));
            if(S('document') == 'all')
                $where .= " and d.columntype='document' ";
            else
                $where .= " and d.columntype='document' and d.smalltype='".S('document')."'";
        }

        $modelTmp = M('document as d inner join '.C('DB_PREFIX').'file as f');
        $info = $this -> flylists($modelTmp , $where , "d.id did,d.*,f.type filetype,f.*" , "" , 'd.ordernum desc , d.id desc');
        foreach($info as $k => $v){
            $resultTmp['id'] = $v['did'];//id
            $resultTmp['fileid'] = $v['fileid'];//文件id
            $resultTmp['title'] = $v['name'];//名称
            $resultTmp['type'] = getColumnTypeName($v["columntype"] , $v["smalltype"]);//所属栏目
            $resultTmp['size'] = $v["size"];//大小
            $resultTmp['ttime'] = date('Y-m-d H:i:s',$v['addtime']);//上传时间
            $resultTmp['uid'] = "admin";//上传人

            $result[] = $resultTmp;
        }
        
        $this -> assign('listinfo' , $result);
        //项目阶段-大类型-小类型___列表
        $CateColumn = C('documentColumn');
        $this -> assign('CateColumn' , $CateColumn);
             
        $this -> display();
    }


    /*
     * 文档保存
     */
    public function documentsave(){
        //记录行为
        action_log('PC', 'Document_documentsave', UID, UID);
        //
        //判断用户级别显示
        $this->userauth('json');
        //
        $save = array(
            'name' => I('post.name'),
            'stage' => I('post.stage'),
            'columntype' => I('post.type'),
            'smalltype' => I('post.cate'),
            'fileid' => I('post.file'),
            'status' => 1,
            'projectid' => S("projectid"),
        );
        //匹配
        $info = M('document') -> where($save) -> find();
        if($info && !$_POST['id'])  $this -> ferror("不能重复上传");
        //添加元素
        $save['uid'] = $_SESSION["userid"];
        $save['ordernum'] = 1;
        $save['status'] = 1;
        //转换状态：1刚上传，2转换中，3完成，4失败
        $save['convertstatus'] = 2;
        $save['filetype'] = getfiletype($save["fileid"]);

        //start
        if(I('post.id')){
            $save['updatetime'] = time();
            M('document') -> where(array('id'=>I('post.id'))) -> save($save);
            $this -> fsucces('编辑成功');
        }else{
            $save['addtime'] = time();
            $newID = M("document") -> add($save);
            /*********************************** ADN开始-该项目没有ADN **********************************
            if($save['filetype']=='rvt' || $save['filetype']=='nwd' || $save['filetype']=='nwc' || $save['filetype']=='dwg')
                A('Home/Adn') -> adnchange($newID);
            /*********************************** ADN结束 **********************************/
            $this -> fsucces('上传成功');
        }
    }
    

    /*
     * 上传大小栏目联动
     */
    public function CateColumnSelect(){
        $documentColumn = C('documentColumn');
        $ColumnInfo =  $documentColumn[I('get.documentColumn')];
        $this -> fsucces($ColumnInfo);
    }


    /*
     * 文档删除
     */
    public function documentdelete(){
        //记录行为
        action_log('PC', 'Document_documentdelete', UID, UID);
        //
        //判断用户级别显示
        $this->userauth('json');
        //
        M('document') -> where(array('id'=>I('post.id'))) -> save(array('status'=>-1));
        $this -> fsucces("删除成功");
    }


    /*
     * 在线打开
     */
    public function onlineshow(){
        $id = I('get.documentid');
        $info = M('document') -> where(array('id'=>$id)) -> find();
        if($info['convertstatus']!=3){
           $this->assign('error',true);
           if($info['convertstatus']==2)  $this->assign('errormsg','转换中');
           if($info['convertstatus']==4)  $this->assign('errormsg','转换失败');
        }  
        //三维
        if ($info['filetype']=='rvt') {
            $src = $this -> path.$info['adnpath'];
        //三维
        } else if($info['filetype']=='nwc') {
            $src = $this -> path.$info['adnpath'];
        //三维
        } else if($info['filetype']=='nwd') {
            $src = $this -> path.$info['adnpath'];
        //二维
        } else if($info['filetype']=='dwg') {
            $src = $this -> path.$info['adnpath'];
        //文件
        } else {
            $info = M('file') -> field('*') -> where(array('id'=>$info['fileid'])) -> find();
            $url = __ROOT__.'/'.$info['allpath'];
            redirect($url);
        }
        $this -> assign('src' , $src);
        $this -> display('modeshow');
    }


    /*
     *
     */

}
